use Term::ReadKey;
use Text::Wrap;
use Term::ReadKey;

package main;


#output "some text" will print some text and a line break
#output "some text",1 will print some text and add two line breaks
#output "some text",2 will print some text without any line breaks
#if the game is started with option -d, output will write everything to a log file

sub output
{

  my $output=shift;
  my $para=0;  # per default the output is not at the end of a paragraph
  my $wait=0;
  
  my ($width, $height, $wpixels, $hpixels) = (100,100,100,100);
  $::cgi or ($width, $height, $wpixels, $hpixels)=&Term::ReadKey::GetTerminalSize();
 $Text::Wrap::columns = $width;

  @_ and $para=shift;
  @_ and $wait=shift;

  $::cgi or print wrap("","",$output);
  $::cgi and return $output;
  #print $output;

  if($wait)
  {
    ReadMode 4; # Turn off controls keys
    my $key;
    while (not defined ($key = ReadKey(-1)))
    {
        # No key yet
    }
    ReadMode 0; # Reset tty mode before exiting
  }

  unless($para eq 2)
  {
    print "\n";
    $para eq 1 and print "\n";
  }
  
  $::game{debug} and logthis($output);

}

sub input
{
  my $input = <STDIN>;
  return $input;  
}

sub debug
{
  my $output = shift;

  output $output if $::game{debug}
}

sub logthis
{
   my $file = "debug.txt";
   open(LOG,'>>'.$file);
   print LOG localtime(time), $_;
}
1;